package com.ruoyi.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.utils.file.QiniuUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PageResource;
import com.ruoyi.system.service.IPageResourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 页面资源Controller
 *
 * @author ruoyi
 * @date 2025-03-30
 */
@RestController
@RequestMapping("/system/pageResource")
public class PageResourceController extends BaseController {
    @Autowired
    private QiniuUtils qiniuUtils;
    @Autowired
    private IPageResourceService pageResourceService;

    /**
     * 上传资源
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:upload')")
    @Log(title = "页面资源", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        try {
            String fileName = UUID.randomUUID().toString() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            // 上传文件到七牛云
            String url = qiniuUtils.upload(file, fileName);
            AjaxResult ajax = AjaxResult.success();
            // 构建返回结果
            ajax.put("url", url);
            ajax.put("fileName", file.getOriginalFilename());
            ajax.put("fileSize", file.getSize());
            ajax.put("fileType", file.getContentType());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查询页面资源列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:list')")
    @GetMapping("/list")
    public TableDataInfo list(PageResource pageResource) {
        startPage();
        List<PageResource> list = pageResourceService.selectPageResourceList(pageResource);
        return getDataTable(list);
    }

    /**
     * 导出页面资源列表
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:export')")
    @Log(title = "页面资源", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PageResource pageResource) {
        List<PageResource> list = pageResourceService.selectPageResourceList(pageResource);
        ExcelUtil<PageResource> util = new ExcelUtil<PageResource>(PageResource.class);
        util.exportExcel(response, list, "页面资源数据");
    }

    /**
     * 获取页面资源详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pageResourceService.selectPageResourceById(id));
    }

    /**
     * 新增页面资源
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:add')")
    @Log(title = "页面资源", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PageResource pageResource) {
        // TODO 临时处理资源类型
        if (pageResource.getResourceType() == null) {
            pageResource.setResourceType("image"); // Set an appropriate default value based on your system requirements
        }
        return toAjax(pageResourceService.insertPageResource(pageResource));
    }

    /**
     * 修改页面资源
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:edit')")
    @Log(title = "页面资源", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PageResource pageResource) {
        return toAjax(pageResourceService.updatePageResource(pageResource));
    }

    /**
     * 删除页面资源
     */
    @PreAuthorize("@ss.hasPermi('system:pageResource:remove')")
    @Log(title = "页面资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pageResourceService.deletePageResourceByIds(ids));
    }
}
